﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Calc
{
    public partial class frmRoofCalculator : Form
    {
        public frmRoofCalculator()
        {
            InitializeComponent();
        }
        int HipValley = 0;
        int iSquares = 0;
        double Measurement = 0;
        double Pitch = 0;
        ManageItems objManageItems = new ManageItems();
        ProjectDB objProjectDB = new ProjectDB();
        ProjectEntities objProjectEntities = new ProjectEntities();

        private void txtMeasurement1_Leave(object sender, EventArgs e)
        {
            if (txtMeasurement1.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement1.Text);
                txtMeasurement2.Text = txtMeasurement1.Text;
            }
            if (txtPitch1.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch1.Text);
                txtPitch2.Text = txtPitch1.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }

            iSquares = 0;

            decimal MPitch=Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
            MPitch = decimal.Round(MPitch, 2);
            //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            txtMeasurementPitch.Text = MPitch.ToString();
            txtLengthPitch.Text = MPitch.ToString();
        }

        private void txtPitch1_Leave(object sender, EventArgs e)
        {
            if (txtMeasurement1.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement1.Text);
                txtMeasurement2.Text = txtMeasurement1.Text;
            }
            if (txtPitch1.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch1.Text);
                txtPitch2.Text = txtPitch1.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            iSquares = 0;
            decimal MPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
            MPitch = decimal.Round(MPitch, 2);
            //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            txtMeasurementPitch.Text = MPitch.ToString();
            txtLengthPitch.Text = MPitch.ToString();
        }

        public string CalculateRoofValue(double dfValue,double fPitch,int HipValleyFlag,int iSquaresFlag)
        {
            double dfNewValue = 0;
            if (HipValleyFlag == 1)
            {
                double dfPitchSqared = fPitch * fPitch;
                dfNewValue = System.Math.Sqrt(dfPitchSqared + (16.971 * 16.971));
                dfNewValue = dfNewValue / 16.971 * dfValue;
            }
            else
            {
                fPitch = fPitch / 12.0;
                double dfFactor = System.Math.Sqrt((fPitch * fPitch) + 1.0);
                dfNewValue = dfValue * dfFactor;
            }
            if (iSquaresFlag == 1)
            {
                dfNewValue = dfNewValue / 100.0;
            }
            return dfNewValue.ToString();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (txtMeasurement1.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement1.Text);
                txtMeasurement2.Text = txtMeasurement1.Text;
            }
            if (txtPitch1.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch1.Text);
                txtPitch2.Text = txtPitch1.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            iSquares = 0;
            decimal MPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
            MPitch = decimal.Round(MPitch, 2);
            //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            txtMeasurementPitch.Text = MPitch.ToString();
            txtLengthPitch.Text = MPitch.ToString();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (txtMeasurement1.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement1.Text);
                txtMeasurement2.Text = txtMeasurement1.Text;
            }
            if (txtPitch1.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch1.Text);
                txtPitch2.Text = txtPitch1.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            iSquares = 0;
            decimal MPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
            MPitch = decimal.Round(MPitch, 2);
            //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            txtMeasurementPitch.Text = MPitch.ToString();
            txtLengthPitch.Text = MPitch.ToString();
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (txtMeasurement2.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement2.Text);
                txtMeasurement1.Text = txtMeasurement2.Text;
            }
            if (txtPitch2.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch2.Text);
                txtPitch1.Text = txtPitch2.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            if (radioButton4.Checked == true)
            {
                iSquares = 1;
            }
            else
            {
                iSquares = 0;
            }
            if (radioButton3.Checked == true)
            {
                iSquares = 0;
            }
            if (iSquares != 1)
            {
                //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                decimal MeaPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                MeaPitch = decimal.Round(MeaPitch, 2);
                txtMeasurementPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                txtLengthPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            }
            else
            {
                decimal Pvalue = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                txtPitchValue.Text = Pvalue.ToString();
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (txtMeasurement2.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement2.Text);
                txtMeasurement1.Text = txtMeasurement2.Text;
            }
            if (txtPitch2.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch2.Text);
                txtPitch1.Text = txtPitch2.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            if (radioButton4.Checked == true)
            {
                iSquares = 1;
            }
            else
            {
                iSquares = 0;
            }
            if (radioButton3.Checked == true)
            {
                iSquares = 0;
            }
            if (iSquares != 1)
            {
                //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                decimal MeaPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                MeaPitch = decimal.Round(MeaPitch, 2);
                txtMeasurementPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                txtLengthPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            }
            else
            {
                decimal Pvalue=Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                txtPitchValue.Text = Pvalue.ToString();
            }
        }

        private void txtMeasurement2_Leave(object sender, EventArgs e)
        {
            if (txtMeasurement2.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement2.Text);
                txtMeasurement1.Text = txtMeasurement2.Text;
            }
            if (txtPitch2.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch2.Text);
                txtPitch1.Text = txtPitch2.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            if (radioButton4.Checked == true)
            {
                iSquares = 1;
            }
            else
            {
                iSquares = 0;
            }
            if (radioButton3.Checked == true)
            {
                iSquares = 0;
            }
            if (iSquares != 1)
            {
                //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                decimal MeaPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                MeaPitch = decimal.Round(MeaPitch, 2);
                txtMeasurementPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                txtLengthPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            }
            else
            {
                decimal Pvalue = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                txtPitchValue.Text = Pvalue.ToString();
            }
        }

        private void txtPitch2_Leave(object sender, EventArgs e)
        {
            if (txtMeasurement2.Text != "")
            {
                Measurement = Convert.ToDouble(txtMeasurement2.Text);
                txtMeasurement1.Text = txtMeasurement2.Text;
            }
            if (txtPitch2.Text != "")
            {
                Pitch = Convert.ToDouble(txtPitch2.Text);
                txtPitch1.Text = txtPitch2.Text;
            }
            if (radioButton2.Checked == true)
            {
                HipValley = 1;
            }
            else
            {
                HipValley = 0;
            }
            if (radioButton1.Checked == true)
            {
                HipValley = 0;
            }
            if (radioButton4.Checked == true)
            {
                iSquares = 1;
            }
            else
            {
                iSquares = 0;
            }
            if (radioButton3.Checked == true)
            {
                iSquares = 0;
            }
            if (iSquares != 1)
            {
                //txtPitchValue.Text = CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                decimal MeaPitch = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                MeaPitch = decimal.Round(MeaPitch, 2);
                txtMeasurementPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
                txtLengthPitch.Text = MeaPitch.ToString();//CalculateRoofValue(Measurement, Pitch, HipValley, iSquares);
            }
            else
            {
                decimal Pvalue = Convert.ToDecimal(CalculateRoofValue(Measurement, Pitch, HipValley, iSquares));
                txtPitchValue.Text = Pvalue.ToString();
            }
        }

        private void btnCancel1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnAccetp2_Click(object sender, EventArgs e)
        {
            
        }

        private void btnAccept1_Click(object sender, EventArgs e)
        {
            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());

            SqlCeCommand cmd = new SqlCeCommand("Update Measurements set CorrectedMeasurement=" +Convert.ToDecimal(txtLengthPitch.Text) + " where MeasurementID=" + ProjectDB.MeasurementID, cn);
            SqlCeCommand cmd1 = new SqlCeCommand("Update MeasurementChild set CorrectedMeasurement=" + Convert.ToDecimal(txtLengthPitch.Text) + " where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            cn.Close();
            this.Dispose();
            frmSelectItems objSelectItems = new frmSelectItems();
            objSelectItems.LoadMeasurements();
            objSelectItems.Refresh();
        }
    }
}
